import cv2, time, random, os, datetime
import os, sys, pyautogui, traceback
import numpy as np
import mss
import action
import random
import auto_player as player
from account import *

# from datetime import datetime
mode = 1
log = []
last_img = ""
# 检测系统
print('操作系统:', sys.platform)
if sys.platform == 'darwin':
    scalar = True
else:
    scalar = False

# 读取文件 精度控制   显示名字
imgs = action.load_imgs()
# pyautogui.PAUSE = 0.05
pyautogui.FAILSAFE = False

start_time = time.time()
# print('程序启动，现在时间', time.ctime())

# 截屏，并裁剪以加速
upleft = (0, 0)
if scalar == True:
    downright = (1136, 700)
else:
    downright = (1900, 1080)
a, b = upleft
c, d = downright
monitor = {"top": b, "left": a, "width": c, "height": d}
# print(monitor)
start = time.time()
accounts = accounts_sub
actions = ['takeTili', 'yinyangliao']

# constants
last_click = None
h = datetime.datetime.now().hour
w = datetime.datetime.now().isoweekday()


# 以上启动，载入设置
##########################################################
def select_mode():
    global start
    end = time.time()
    hours, rem = divmod(end - start, 3600)
    minutes, seconds = divmod(rem, 60)
    print("运行时间：{:0>2}:{:0>2}:{:05.2f}".format(int(hours), int(minutes), seconds))
    print(datetime.datetime.now())
    #a = findI("zhanji_600")
    #print(a)
    #return
    # click("sub_account_fengshui_600")
    # return

    # fengmo()
    # yinyangliao()
    # yinyangliao()
    # common_task()
    # yyl_jiyang()
    if action_sub == "huanhao":
        huanhao()
    if action_sub == "yyl_jiyang":
        yyl_jiyang()
    if action_sub == "common_task":
        common_task()
    if action_sub == "fengmo":
        fengmo("fengmo")

    # print(log)
    # click("mg163")
    # click("yinyangliao_1_guanbi", 2)
    # click("sub_account_2_2_lvshui")
    # click("sub_account_2_3_anning_xiao")
    # click("sub_account_2_4_hexi")

    return


def switchAccount(account, sub_account):
    if switch(account, sub_account):
        log.append(account)
        log.append(sub_account)
        common_task()
        yinyangliao()
        fengmo(sub_account)
        xuanshang(account, sub_account)
        print(log)
        log.clear()
    return


def click3(ww1=0, ww2=0, hh1=0, hh2=0, sec=0):
    ww = random.randint(ww1, ww2)
    hh = random.randint(hh1, hh2)
    t2 = random.randint(100, 120) / 100
    time.sleep(t2)
    action.click([ww, hh])
    t2 = random.randint(140, 160) / 100
    time.sleep(t2 + sec)
    print("\x1B[32m|______________________" + last_img + "：\x1B[0m")
    print("\x1B[32m|______________________" + str(ww1) + "：\x1B[0m")
    return True


def findI(img):
    if player.find_touch(img, False):
        print("检查" + img)
        return True


def click(img, sec=0, break_count=3):
    for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25]:
        last_img = img
        time.sleep(1.5)
        if player.find_touch(img):
            print("\x1B[32m|______________________" + img + "：\x1B[0m")
            t2 = random.randint(140, 160) / 100
            time.sleep(t2 + sec)
            time.sleep(1.5)

            return True
        else:
            if i == break_count:
                print("\x1B[37m|___________没有找到" + img + "！！！！！：\x1B[0m")
                return False
            time.sleep(.5)


# 悬赏
def xuanshang(account, sub_account):
    if not xuanshang_switch:
        return True
    if click("xuanshang"):

        a = "adb shell screencap -p sdcard/screen" + sub_account + ".jpg"
        b = "adb pull sdcard/screen" + sub_account + ".jpg ./screen/BaiduSyncdisk > null"
        for row in [a, b]:
            time.sleep(0.1)
            os.system(row)
    if not click("xuanshangguanbi"):
        click("xuanshangguanbi")


# 领取体力
def yyl_tili():
    print("|_______开始领取yyl体力：")
    last = False
    if click("yyl_tili_1"):  # 点击体力
        if click("yyl_tili_2_take"):  # 取出体力
            if click("yyl_tili_3_close"):  # 关闭蒙板
                last = click("yyl_tili_4_close")  # 关闭领取体力
    if last:
        print("\x1B[32m|___________领取yyl体力成功：\x1B[0m")
        log.append('yyl——领取体力成功')
    else:
        print("\x1B[31m|___________o(╥﹏╥)o领取yyl体力失败：\x1B[0m")
        log.append('yyl——领取体力失败')


# 领取勾玉
def yyl_gouyu():
    print("|_______开始领取yyl勾玉：")
    last = click("yyl_gouyu_1")
    if last:
        print("\x1B[32m|___________领取yyl勾玉成功：\x1B[0m")
        log.append('yyl——领取勾玉成功')
    else:
        print("\x1B[31m|___________o(╥﹏╥)o领取yyl勾玉失败：\x1B[0m")
        log.append('yyl——领取勾玉失败')


# 放结界卡
def yyl_put_card():
    print("|_______开始放结界卡：")
    click("yyl_put_card_1_begin")  # 开始放结界卡
    click("yyl_put_card_2_all")  # 点击全部
    click("yyl_put_card_3_select_all_taigu")  # 选择所有太鼓
    click("yyl_put_card_4_select_one_taigu")  # 选择其中一个太鼓
    click("yyl_put_card_5_active_taigu")  # 激活太鼓
    last = click("yyl_put_card_6_end")  # 关闭
    if last:
        print("\x1B[32m|___________放结界卡成功：\x1B[0m")
        log.append('yyl——放结界卡成功')
    else:
        print("\x1B[31m|___________o(╥﹏╥)o放结界卡失败：\x1B[0m")
        log.append('yyl——放结界卡失败')


# 寄养
def yyl_jiyang():
    print("|_______开始寄养：")
    if click("yyl_jiyang_1_begin"):  # 寄养开始
        if click("yyl_jiyang_2_entry", 5):  # 进入寄养
            success = click("yyl_jiyang_3_select_gouyu")  # 选择勾玉
            if not success:
                success = click("yyl_jiyang_4_select_tili")  # 选择体力
            if not success:
                success = click("yyl_jiyang_5_select_jinbi")  # 选择金币
            if success:
                click("yyl_jiyang_6_entry_jiejie", 5)
                click3(250, 320, 500, 600, 2)
                click("yinyangliao_12_5_fangshishenjieshu", 1)
                click("yinyangliao_12_6_fanhui", 10)
                click("yinyangliao_12_7_fanhui", 3, 10)
                print("\x1B[32m|___________寄养成功：\x1B[0m")
                log.append('yyl——寄养成功')

            else:
                click3(1300, 1400, 100, 500, 6)
                click("yinyangliao_12_6_fanhui", 10)
                print("\x1B[31m|___________o(╥﹏╥)o寄养失败：\x1B[0m")
                log.append('yyl——寄养失败')
        else:
            click("yinyangliao_12_6_fanhui", 10)
    click("yinyangliao_12_6_fanhui", 10)
    if not click("yinyangliao_12_7_fanhui", 3, 10):
        click3(105, 135, 16, 42)


def yinyangliao():
    if not yinyangliao_switch:
        return True
    # click("yinyangliao_1_zhankai", 2)
    # mutil_click(["yinyangliao_1_zhankai","yinyangliao_1_zhankai_1","yinyangliao_1_zhankai_2"], 2)
    # click("yinyangliao_2_jinru", 4)
    # click("yinyangliao_3_jinrujiejie", 4)

    # if not findI("yinyangliao_1_guanbi"):
    # 展开
    click3(1370, 1400, 640, 670, 2)
    # 进入阴阳寮
    click3(615, 659, 614, 660, 4)
    # 进入结界
    click3(1070, 1120, 628, 678, 4)
    yyl_tili()
    yyl_gouyu()
    yyl_put_card()
    yyl_jiyang()
    # fengmo()
    # if not click("yinyangliao_12_6_fanhui", 10):
    #     click3(105, 130, 30, 57, 5)
    # if not click("yinyangliao_16_fanhui", 4):
    #     click3(105, 130, 20, 40, 5)
    # 关闭
    # if not click("yinyangliao_1_guanbi"):
    click3(1370, 1400, 640, 670, 2)


# 点击不好确认的图片
def mutil_click(imgs, sec=0, break_count=3):
    # random.shuffle(imgs)
    for img in imgs:
        success = click(img, sec, break_count)
        if success:
            return success
    return False


def switch(account, sub_account):
    print("|___开始换号：" + account + "[" + sub_account + "]")
    t = random.randint(4, 5)
    time.sleep(t)
    success = False
    for a in [1, 2, 3]:
        for i in ['switch_account_1_1_avater', 'switch_account_1_2_avater', 'switch_account_1_3_avater',
                  'switch_account_1_4_avater']:  # 点击头像
            if click(i):
                break
        if click("switch_account_2_user_center", 6):  # 用户中心
            success = True
            break
    if not success:
        return False
    imgs = ["switch_account_3_switch_account_6"]
    # mutil_click(imgs, 2, 3)  # 点击切换账号按钮
    click3(1250, 1400, 150, 210, 3)  # 点击切换账号按钮
    # click("switch_account_3_switch_account", 2)  # 点击切换账号按钮
    if not click("switch_account_4_click_wangyi"):  # 点击网易图标选择账号
        return False
    click(account)  # 选择主账号
    if not click("switch_account_5_login", 8):  # 登录
        return False
    if mode == 1:
        imgs = ["switch_account_6_switch", "switch_account_6_1_switch", "switch_account_6_2_switch"
            , "switch_account_6_3_switch", "switch_account_6_4_switch", "switch_account_6_5_switch"]
        mutil_click(imgs, 0, 25)  # 开始切换
    else:
        click3(560, 878, 508, 535, 2)
    t = random.randint(5, 6)
    time.sleep(t)
    if not click("sub_account_" + sub_account):  # 选择子账号名
        return False
    if not click("sub_account_" + sub_account + "_f"):  # 选择子账号服务器
        return False
    if mode == 1:
        click3(700, 870, 560, 600, 2)  # 点击进入游戏
        # imgs = ["switch_account_7_1_entry", "switch_account_7_2_entry", "switch_account_7_3_entry"
        #     , "switch_account_7_4_entry"]
        # mutil_click(imgs, 0, 25)  # 点击进入游戏
    else:
        click3(700, 870, 560, 600, 2)  # 点击进入游戏
    t = random.randint(6, 8)
    time.sleep(t)
    # 'gm163': ['dasewan55', 'dasewan60'],
    # 'mg163': ['zongbei57', 'zongbei60'],
    if sub_account != '2_3_anning_xiao' and sub_account != "1_3_wuming" and sub_account != "dasewan55_510" and sub_account != "dasewan60" and sub_account != "zongbei57" and sub_account != "zongbei60" and sub_account != "chuqi_600" and sub_account != "anquanfazhan60" and sub_account != "xingxingxing_500" and sub_account != "fengshui_600":
        click("sub_account_" + sub_account + "_avater")
        t = random.randint(13, 15)
        if mode == 1:
            click3(700, 870, 560, 600, 2)  # 点击屏幕
            # success = click("switch_account_8_click_screen", t)  # 点击屏幕
        else:
            # click3(700, 870, 560, 600, 2)  # 点击屏幕
            success = True
            t = random.randint(15, 17)
        time.sleep(t)
    else:
        t = random.randint(15, 17)
        time.sleep(t)
        success = True
    if w == 3:
        click3(1282, 1300, 80, 90, 2)  # 点击屏幕
        click3(1282, 1300, 80, 90, 2)  # 点击屏幕
    if success:
        print("\x1B[32m|___________切换账号成功：\x1B[0m")
        return True
    else:
        print("\x1B[31m|___________o(╥﹏╥)o切换账号失败！！！！！：\x1B[0m")
        return False


def common_task():
    if not common_task_switch:
        return True

    random.shuffle(actions)
    # 签到
    print("|_______开始签到：")
    if click("qiandao"):
        if click("qiandao2", 2):
            if click("qiandao3"):
                print("\x1B[32m|___________签到成功：\x1B[0m")
                log.append('签到成功')
    else:
        print("\x1B[31m|___________签到失败：\x1B[0m")
        log.append('签到失败')

    if h == 12 or h == 13 or h == 20 or h == 21:
        print("|_______开始领取体力：")
        if click("takeTili"):
            imgs = ["takeTili2", "takeTili2_1"];
            if mutil_click(imgs):
                print("\x1B[32m|___________领取体力成功：\x1B[0m")
                log.append('领取体力成功')
            else:
                print("\x1B[31m|___________o(╥﹏╥)o领取体力失败：\x1B[0m")
                log.append('领取体力失败')
        else:
            print("\x1B[31m|___________o(╥﹏╥)o领取体力失败：\x1B[0m")
            log.append('领取体力失败')
    # 领取勾玉
    if click("takeGouYu"):
        if mode == 1:
            click3(700, 870, 560, 600, 2)  # 关闭勾玉
        else:
            click3(700, 870, 560, 600, 2)  # 关闭勾玉
        print("\x1B[32m|___________领取勾玉成功：\x1B[0m")
        log.append('领取勾玉成功')
    else:
        # 领取勾玉
        # click3(868, 880, 413, 440)
        # 关闭勾玉奖励
        # click3(1100, 1300, 450, 500)
        print("\x1B[31m|___________领取勾玉失败：\x1B[0m")
        log.append('领取勾玉失败')

    # ['takeGouYu','takeJiaCheng','takeTili','yinyangliao',fengmo]
    for i in actions:
        if i == 'takeGouYu':
            print("|_______开始领取勾玉：")
            # 领取勾玉
            if click("takeGouYu"):
                if mode == 1:
                    mutil_click("takeGouYu_2", "takeGouYu_3")  # 关闭勾玉
                else:
                    click3(700, 870, 560, 600, 2)  # 关闭勾玉
                print("\x1B[32m|___________领取勾玉成功：\x1B[0m")
                log.append('领取勾玉成功')
            else:
                print("\x1B[31m|___________o(╥﹏╥)o领取勾玉失败：\x1B[0m")
                log.append('领取勾玉失败')
        if i == 'takeJiaCheng' and (w == 6 or w == 7 or w == 1):
            print("|_______开始领取加成：")
            if mode == 1:
                if click(i):
                    click("querenlingqu")
                    success = click("tingyuan")
                    if success:
                        print("\x1B[32m|___________领取加成功：\x1B[0m")
                        log.append('领取加成功')
                    else:
                        print("\x1B[31m|___________o(╥﹏╥)o领取加失败：\x1B[0m")
                        log.append('领取加失败')

        if i == 'takeTili' and (h == 12 or h == 13 or h == 20 or h == 21):
            print("|_______开始领取体力：")
            if click(i):
                imgs = ["takeTili2", "takeTili2_1"];
                if mutil_click(imgs):
                    print("\x1B[32m|___________领取体力成功：\x1B[0m")
                    log.append('领取体力成功')
            else:
                print("\x1B[31m|___________o(╥﹏╥)o领取体力失败：\x1B[0m")
                log.append('领取体力失败')
        if i == 'email' and ( w == 8):
            print("|_______开始领取邮箱：")
            if click("email"):
                if click("email2"):
                    if click("email3"):
                        if click("email4"):
                            if click("email5"):
                                print("\x1B[32m|___________领取邮箱成功：\x1B[0m")
                                log.append('领取邮箱成功')

                else:
                    click("email5")
                    print("\x1B[31m|___________o(╥﹏╥)o领取邮箱失败：\x1B[0m")
                    log.append('领取邮箱失败')
        if i == 'qiandao':
            print("|_______开始签到：")
            if click("qiandao"):
                if click("qiandao2"):
                    if click("qiandao3"):
                        print("\x1B[32m|___________签到成功：\x1B[0m")
                        log.append('签到成功')
            else:
                print("\x1B[31m|___________o(╥﹏╥)o签到失败：\x1B[0m")
                log.append('签到失败')


def huanhao():
    for account, values in accounts.items():
        for subAccount in values:
            switchAccount(account, subAccount)


########################################################
def fengmo(sub_account):
    if not fengmo_switch:
        return True
    if not (h == 17 or h == 18 or h == 19 or h == 20 or h == 21):
        return
    # if not (sub_account == "dasewan55" or sub_account == "dasewan60" or sub_account == "zongbei57" or sub_account == "zongbei60") :
    #    return
    # if not click("fengmo_1_start", 0):  # 封魔入口
    #     if not click("fengmo_2_douji", 0):  # 如果封魔入口图标换成斗技图标
    #         if not click("fengmo_3_douji", 0):  # 如果封魔入口图标换成斗技图标
    #             print("没有找到入口")
    #             return
    # click("fengmo_3_canjia")  # 参加封魔
    # # click("fengmo_4_qianwang")  # 前往封魔
    # click("fengmo_5_dianji")  # 点击第一次
    # t = random.randint(2, 3)
    # time.sleep(t)
    # click("fengmo_5_dianji")  # 点击第二次
    # t = random.randint(2, 3)
    # time.sleep(t)
    # click("fengmo_5_dianji")  # 点击第三次
    # t = random.randint(2, 3)
    # time.sleep(t)
    # click("fengmo_5_dianji")  # 点击第四次
    # t = random.randint(2, 3)
    # time.sleep(t)
    # for i in [1, 2]:  # 再随机点1到2次
    #     click("fengmo_5_dianji")
    #     if i == random.randint(1, 2):
    #         break
    # click("fengmo_6_jiangli")  # 领取奖励
    # click("fengmo_7_lingqujianglichenggong")  # 领取成功
    # 封魔入口
    click3(72, 73, 284, 310)
    # 参加封魔
    click3(400, 700, 150, 250)
    # 前往封魔
    click3(720, 780, 500, 535, 4)
    # 点击第一次
    click3(1320, 1370, 620, 670, 4)
    # 点击第一次
    click3(1320, 1370, 620, 670, 4)
    # 点击第一次
    click3(1320, 1370, 620, 670, 4)
    # 点击第一次
    click3(1320, 1370, 620, 670, 4)
    # 点击第一次
    click3(1320, 1370, 620, 670, 4)

    # 领取奖励
    click3(1380, 1410, 220, 250, 4)
    # 关闭领取
    click3(310, 450, 40, 54, 5)
    # 返回
    # click3(110, 150, 40, 54, 5)
    if (
            sub_account == "dasewan55" or sub_account == "dasewan60" or sub_account == "zongbei57" or sub_account == "zongbei60" or sub_account == "1_1_piaoliu" or sub_account == "1_2_qingshan" or sub_account == "2_1_anning" or sub_account == "2_2_lvshui" or fengmo_xiaohao_switch):

        for i in [1, 2, 3]:
            # click("fengmo_8_xunzhaoshouling")  # 寻找首领
            click3(1185, 1200, 660, 680)
            # dayOfWeek = datetime.datetime.now().isoweekday()
            click3(750, 777, 332, 367)
            # click("fengmo_9_guaishou" + str(dayOfWeek))  # 点击首领
            # click("fengmo_10_jijietiaozhan")  # 集结挑战
            click3(1200, 1270, 570, 630)
            # click("fengmo_11_jijiequeding")  # 集结确定
            click3(820, 940, 410, 440)
            t = random.randint(2, 3)
            time.sleep(t)
            meiyouzhaodao = mutil_click(["fengmo_11_1_meiyouzhaodaoguaishou", "fengmo_11_1_meiyouzhaodaoguaishou_2",
                                         "fengmo_11_1_meiyouzhaodaoguaishou_3"])  # 寻找下个首领
            if not meiyouzhaodao:
                break
        click("fengmo_11_jijiequeding")  # 开始战斗
        if w == 1:
            time.sleep(random.randint(200, 220))
        t2 = random.randint(20, 21)
        for i in [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,22,23,24,25,26,27,28,29]:
            if findI("zhanji_600") or findI("fengmoshengli") or findI("fengmo_12_chenggong") or findI("fengmo_12_1_shengli"):
                click3(615, 659, 614, 660)
                break
            else:
                click3(615, 659, 614, 660)
                time.sleep(t2)


        click3(615, 659, 614, 660)
        click3(615, 659, 614, 660)
        click3(615, 659, 614, 660)

        click("fengmo_12_1_shengli")
        click("fengmo_12_chenggong")
        click("fengmo_13_fanhui")
        click("fengmo_13_chenggong")
        click("fengmo_14_chenggong")
        click3(615, 659, 614, 660)
    click("fengmo_15_fanhui")
    click("fengmo_16_quedingtuicu")
    if not click("fengmo_18_fanhui", 5, 10):
        click3(120, 140, 40, 55, 3)


####################################################
if __name__ == '__main__':
    select_mode()
